<?php
class Simpple_Model_Table {
	var $name;
	var $primaryKey;
	/**
	 * @var Simpple_Model_Interface
	 */
	private $db;
	
	/**
	 * @var Simpple_Model_Interface
	 */
	private static $defaultDb;
	
	/**
	 * @param Simpple_Model_Interface $db
	 */
	function __construct($db=null) {
		if ($db) {
			$this->db = $db;
		}
	}
	
	/**
	 * 设置默认db
	 *
	 * @param Simpple_Model_Interface $db
	 */
	public static function setDefaultDb($db) {
		self::$defaultDb = $db;
	}
	/**
	 * 获取默认db
	 *
	 * @return Simpple_Model_Interface
	 */
	public static function getDefaultDb() {
		return self::$defaultDb;
	}
	
	/**
	 * 获取对象
	 *
	 * @param int | string | array $id
	 * @return array | false
	 */
	function get($id) {
		$options = $this->getIdWhereOptions($id);
		$db = $this->getDb();
		$sql = "select * from `{$this->name}` where ".$db->buildWhere($options);
		$ret = $db->query($sql)->toArray();
		if (!$ret) return false;
		return $ret;
	}
	
	/**
	 * 删除对象
	 *
	 * @param int | string | array $id
	 * @param int 受影响的记录数
	 */
	function delete($id) {
		$options = $this->getIdWhereOptions($id);
		$db = $this->getDb();
		$sql = "delete from `{$this->name}` where ".$db->buildWhere($options);
		$res = $db->execute($sql);
		return $db->getLastId();
	}
	
	/**
	 * 插入数据
	 *
	 * @param array $data
	 * @return int
	 */
	function insert($data) {
		$db = $this->getDb();
		return $db->insert($this->name, $data);
	}
	
	/**
	 * 更新数据
	 *
	 * @param string | array $where
	 * @param array $data
	 * @param bool $ignore
	 * @return int
	 */
	function update($where, $data, $ignore = false) {
		$db = $this->getDb();
		if (is_array($where)) {
			$where = $db->buildWhere($where);
		}
		$sql = "UPDATE `{$this->name}` set ".$this->buildUpdate($data);
		$ret = $db->query($sql);
		return $db->getAffectedRows();
	}
	
	/**
	 * 查询符合条件的数据
	 *
	 * @param string | array $where
	 */
	function find($where=null){
		$db = $this->getDb();
		if (is_array($where)) {
			$where = $db->buildWhere($where);
		}
		$sql = "SELECT * FROM `{$this->name}` where ".$where;
		return $db->query($sql)->toArray();
	}
	
	/**
	 * 创建更新条件
	 *
	 * @param array $data
	 */
	private function buildUpdate($data) {
		$ret = array();
		foreach($data as $key=>$value) {
			if (!preg_match("/[`()]+/", $key)) {
				$key = "`".$key."`";
			}
			if (!is_numeric($value)) {
				$value = "'".$this->escape($value)."'";
			}
			$ret[] = "({$key}={$value})";
		}
		return implode(",", $ret);
	}
	
	/**
	 * 根据id构建查询条件
	 *
	 * @param int | array $id
	 * @return array
	 */
	private function getIdWhereOptions($id) {
		$options = array();
		if (is_array($this->primaryKey)) {
			if (!is_array($id)) {
				throw new Simpple_Model_Exception("id must be array");
			}
			foreach($this->primaryKey as $key) {
				if (!key_exists($key, $id)) {
					throw new Simpple_Model_Exception("primary key of {$key} is not defined");
				}
				$options[$key] = $id[$key];
			}
		} else {
			$options[$this->primaryKey] = $id;
		}
		return $options;
	}
	
	/**
	 * 获取db
	 * @return Simpple_Model_Interface
	 */
	function getDb() {
		if (!$this->db) {
			$this->db = self::getDefaultDb();
		}
		if (!$this->db) {
			throw new Simpple_Model_Exception('the db is not configed');
		}
		return $this->db;
	}
}